package com.lhz.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lhz.pojos.Dingdan;
import com.lhz.pojos.Dingdandetail;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhz.dao.DingDanDao;
import com.lhz.dao.DingDanDetailDao;
import com.lhz.dao.mybatis.ChushiMybatis;

@Service
@Transactional
public class ChushiService {

	/**
	 *                 _ooOoo_
	 *                o8888888o
	 *                88" . "88
	 *                (| -_- |)
	 *                O\  =  /O
	 *             ____/`---'\____
	 *           .'  \\|     |//  `.
	 *          /  \\|||  :  |||//  \
	 *         /  _||||| -:- |||||-  \
	 *         |   | \\\  -  /// |   |
	 *         | \_|  ''\---/''  |   |
	 *         \  .-\__  `-`  ___/-. /
	 *       ___`. .'  /--.--\  `. . __
	 *    ."" '<  `.___\_<|>_/___.'  >'"".
	 *   | | :  `- \`.;`\ _ /`;.`/ - ` : | |
	 *   \  \ `-.   \_ __\ /__ _/   .-` /  /
	 *====`-.____`-.___\_____/___.-`____.-'======
	 *                 `=---='
	 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	 *             佛祖保佑       永无BUG
	 */
	
	@Autowired
	private DingDanDao jpadao;
	
	@Autowired
	private DingDanDetailDao detaijpadao;
	
	@Autowired
	private ChushiMybatis chushi;
	
	/**
	 * 显示后厨界面详细界面
	 * @return
	 */
	public Map<String,Object> searchBychushi(int pageNow,int pageSize){
		Map<String,Object> map=new HashMap<String, Object>();
		PageHelper.startPage(pageNow,pageSize,true);
		List<Dingdan> info = chushi.searchBychushi();
		PageInfo<Dingdan> page=new PageInfo<>(info);
		if(info!=null) {
			map.put("dingdan", info);
			map.put("total", page.getTotal());
		}else {
			map.put("message", "没有查到订单详细相关的信息!..");
			return map;
		}
		return map;
	}
	
	/**
	 * 修改菜单状态
	 * @param dingdanId
	 * @param goodsId
	 * @param status
	 * @return
	 */
	public Map<String,Object> updatechushistatus(Integer dingdanId,Integer status){
		Map<String,Object> map=new HashMap<>();
		if(dingdanId==null||status==null) {
			map.put("message", "没有传过来的参数ID呢!...");
			return map;
		}
		
		Dingdandetail one = detaijpadao.findOne(dingdanId);
		if(one!=null) {
			one.setStatus(status);
			map.put("result", "OK!");	
		}else {
			map.put("message", "没有查到相关信息!");
			throw new RuntimeException("出错了");
			}
 		return map;
	}
	
	/**
	 * 取消订单
	 * @param dingdan
	 * @param status
	 * @return
	 */
	public Map<String,Object> updatecanceldingdan(String dingdan,Integer status){
		Map<String,Object> map=new HashMap<>();
		if(dingdan.isEmpty()||status==null) {
			map.put("message", "没有找到相关信息");
			return map;
		}
		if(status.equals(0)) {
			Integer a=4;
			Dingdan findOne = jpadao.findOne(dingdan);
			if(findOne!=null) {
				findOne.setStatus(a);
				map.put("result", "取消订单成功啦!");
			}else {
				map.put("message", "没有找到这个订单相关信息呢!...");
				throw new RuntimeException("没有找到这个订单相关信息呢!...");
			}
		}
		return map;
	}
	
	/**
	 * 交易完成
	 * @param dingdan
	 * @return
	 */
	public Map<String,Object> updatecanceldingdan(String dingdan){
		Map<String,Object> map=new HashMap<>();
		if(dingdan.isEmpty()) {
			map.put("message", "没有找到相关Id信息");
			return map;
		}
		Dingdan dindan = jpadao.findOne(dingdan);
		if(dindan!=null) {
			List<Dingdandetail> list = dindan.getDingdandetails();
			Integer a=4;
			for (Dingdandetail dingdandetail : list) {
				int status2 = dingdandetail.getStatus();
				if(status2>0) {
					map.put("message", "该订单尚有菜品在制作或未制作，不能完成订单");
					return map;
				}else {
					dindan.setStatus(a);
					map.put("result", "交易完成啦!");
				}
			}
		}else {
			map.put("message", "没有找到订单的相关信息呢!");
			throw new RuntimeException("没有找到订单的相关信息呢!");
		}
		return map;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
